package com.example.chessmigame;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

public class GameActivity1 extends AppCompatActivity {
    public static final String TAG = "Game1";
    public static final int UPDATE = 0x1;
    int a, b, n1;
    int count;
    EditText time;
    TextView num, c1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game1);

        SharedPreferences setting = getSharedPreferences("GameSettings", MODE_PRIVATE);
        String countdown = setting.getString("countdown", "7");
        String n1_str = setting.getString("n1", "7");
        Log.i(TAG, "获取预设值");

        b = Integer.parseInt(countdown);
        n1 = Integer.parseInt(n1_str);

        a = (int) (Math.random() * Math.pow(10, n1));
        Log.i(TAG, "生成随机数" + a);

        num = findViewById(R.id.output1);
        num.setText(Integer.toString(a));

        Intent intent1 = getIntent();
        String count_str = intent1.getStringExtra("count");
        String q;
        if (count_str == null) {//对分数判空
            q = "0";
        } else {
            q = count_str;
        }
        c1 =  findViewById(R.id.c1);
        c1.setText(q+"分");

        time = findViewById(R.id.time1);

        EditText inputnum = findViewById(R.id.input1);
        //inputnum.setVisibility(View.INVISIBLE);
        inputnum.setFocusableInTouchMode(false);
        Log.i(TAG, "不触碰");


        Log.i(TAG, "成功创建页面");

        begin();
        Log.i(TAG, "成功开始倒计时");

    }

    public void submit1(View V) {
        EditText inputnum = findViewById(R.id.input1);
        String inputText = inputnum.getText().toString();

        if (inputText.equals("")) {//防止输入空值
            Toast toastCenter = Toast.makeText(getApplicationContext(), "请输入！", Toast.LENGTH_LONG);
            toastCenter.setGravity(Gravity.CENTER, 0, 0);
            toastCenter.show();
        } else {
            int inputnumber = Integer.parseInt(inputText);
            Log.i(TAG, "随机数" + a + ",输入数" + inputnumber);

            if (inputnumber == a) {//答对了
                Intent intent1 = getIntent();
                String count_str = intent1.getStringExtra("count");
                Log.i(TAG, "答对了");
                if (count_str != null) {//不是第一次答对
                    count = Integer.parseInt(count_str) + 1;
                    String count_str1 = String.valueOf(count);
                    Intent intent2 = new Intent(GameActivity1.this, GameActivity1.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent2.putExtra("count", count_str1);
                    startActivity(intent2);
                } else {//第一次答对
                    count = 1;
                    String count_str1 = String.valueOf(count);
                    Intent intent2 = new Intent(GameActivity1.this, GameActivity1.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent2.putExtra("count", count_str1);
                    startActivity(intent2);
                }
            } else {//答错了
                Intent intent1 = getIntent();
                String count_str = intent1.getStringExtra("count");
                Log.i(TAG, "count_str:" + count_str);
                if (count_str != null) {//不是在第一次游戏时答错
                    count = Integer.parseInt(count_str);
                    String count_str1 = String.valueOf(count);
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("游戏结果")
                            .setMessage("您的答案是：" + inputText + ",正确答案是：" + a + ";您的分数为：" + count_str1)
                            .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(GameActivity1.this, GameActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                }
                            });
                    builder.setNegativeButton("再来一次", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(GameActivity1.this, GameActivity1.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    });
                    builder.create().show();
                } else {//第一次答题就错了
                    count = 0;
                    String count_str1 = String.valueOf(count);
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("游戏结果")
                            .setMessage("您的答案是：" + inputText + ",正确答案是：" + a + ";您的分数为：" + count_str1)
                            .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(GameActivity1.this, GameActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                }
                            });
                    builder.setNegativeButton("再来一次", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(GameActivity1.this, GameActivity1.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        }
                    });
                    builder.create().show();
                }
            }
        }

    }

    public void over(View v) {
        Intent intent1 = getIntent();
        String count_str = intent1.getStringExtra("count");
        String a;
        if (count_str == null) {
            a = "0";
        } else {
            a = count_str;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("游戏结果")
                .setMessage("现在您的分数为：" + a)
                .setPositiveButton("返回游戏选择页面", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(GameActivity1.this, GameActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                });
        builder.setNegativeButton("再来一次", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(GameActivity1.this, GameActivity1.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        builder.create().show();
    }

    private static class MyHandler extends Handler {
        //弱引用
        WeakReference<GameActivity1> weakReference;

        public MyHandler(GameActivity1 activity) {
            weakReference = new WeakReference<GameActivity1>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            GameActivity1 activity = weakReference.get();
            if (activity != null) {
                activity.time.setText(String.valueOf(msg.arg1));
            }
        }
    }

    private MyHandler handler = new MyHandler(this);

    public void begin() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = b; i >= 0; i--) {
                    Message msg = new Message();
                    msg.arg1 = i;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);//休眠1秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i(TAG, " " + i);
                }
                //计时结束后
                Log.i(TAG, "计时结束");
                num.setText("请作答");
                EditText inputnum = findViewById(R.id.input1);
                //inputnum.setVisibility(View.VISIBLE);
                inputnum.requestFocus();
                inputnum.setFocusable(true);
                inputnum.setFocusableInTouchMode(true);
                inputnum.setInputType(InputType.TYPE_CLASS_NUMBER);
                Log.i(TAG, "可触");
            }
        }).start();
    }
}